package com.todo.biz.infrastructure.repository.impl;

import com.todo.common.common.enums.BizErrorCodeEnum;
import com.todo.common.common.exception.BizException;
import com.todo.biz.infrastructure.entity.po.CollectRelationPO;
import com.todo.biz.infrastructure.mapper.CollectRelationMapper;
import com.todo.biz.infrastructure.repository.CollectRelationRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

/**
 * (CollectRelation)表服务实现类
 *
 * @author zhangwenxiang
 * @since 2024-03-04 21:10:03
 */
@Slf4j
@Repository("collectRelationRepository")
public class CollectRelationRepositoryImpl implements CollectRelationRepository {

    public static final Integer MAX_RELATION_COUNT = 100;

    @Resource
    private CollectRelationMapper collectRelationMapper;


    @Override
    public CollectRelationPO queryCollectRelationPOById(Long id) {
        if (id == null || id < 0) {
            throw new BizException(BizErrorCodeEnum.DATA_NOT_EXIST);
        }
        return collectRelationMapper.queryCollectRelationPOById(id);
    }

    @Override
    public Boolean insertCollectRelationPO(CollectRelationPO collectRelationPO) {
        if (collectRelationPO == null) {
            throw new BizException(BizErrorCodeEnum.DATA_NOT_EXIST);
        }
        if (collectRelationPO.getRelationId() == null || collectRelationPO.getSetId() == null) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        Integer count = queryCountBySetId(collectRelationPO.getSetId());
        if (count > MAX_RELATION_COUNT) {
            log.warn("CollectRelationRepository#insertCollectRelationPO 新增集合关联失败，数据过量 集合Id：【{}】", collectRelationPO.getSetId());
            throw new BizException(BizErrorCodeEnum.RELATION_DATA_EXCESSIVE);
        }
        CollectRelationPO alreadyCollectRelationPO = queryCollectRelationByRelation(collectRelationPO.getSetId(), collectRelationPO.getRelationType(), collectRelationPO.getRelationId());
        if (alreadyCollectRelationPO != null) {
            log.info("CollectRelationRepository#insertCollectRelationPO 新增集合关联完成，存在历史数据 ID：【{}】", alreadyCollectRelationPO.getId());
            return true;
        }
        return collectRelationMapper.insertCollectRelationPO(collectRelationPO) > 0;
    }

    @Override
    public Boolean batchInsertCollectRelation(List<CollectRelationPO> collectRelationPOList) {
        if (collectRelationPOList == null || collectRelationPOList.isEmpty()){
            return true;
        }
        return collectRelationMapper.insertBatch(collectRelationPOList) > 0;
    }

    @Override
    public Boolean updateCollectRelationPOById(CollectRelationPO collectRelationPO) {
        if (collectRelationPO == null) {
            throw new BizException(BizErrorCodeEnum.DATA_NOT_EXIST);
        }
        if (collectRelationPO.getRelationId() == null || collectRelationPO.getSetId() == null) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        CollectRelationPO alreadyCollectRelationPO = queryCollectRelationPOById(collectRelationPO.getId());
        if (alreadyCollectRelationPO == null) {
            log.warn("CollectRelationRepository#updateCollectRelationPOById 更新集合关联失败，数据不存在 ID：【{}】", collectRelationPO.getId());
            return false;
        }
        return collectRelationMapper.updateCollectRelationPOById(collectRelationPO) > 0;
    }

    @Override
    public Boolean removeCollectRelationById(Long id) {
        if (id == null || id < 0) {
            throw new BizException(BizErrorCodeEnum.DATA_NOT_EXIST);
        }
        return collectRelationMapper.removeCollectRelationById(id) > 0;
    }

    @Override
    public Integer removeCollectRelationBySetId(Long setId) {
        if (setId == null || setId < 0) {
            throw new BizException(BizErrorCodeEnum.DATA_NOT_EXIST);
        }
        Integer count = queryCountBySetId(setId);
        if (count == 0) {
            return 0;
        }
        return collectRelationMapper.removeCollectRelationBySetId(setId);
    }

    @Override
    public List<Long> queryIdListBySetId(Long setId) {
        if (setId == null || setId < 0) {
            throw new BizException(BizErrorCodeEnum.DATA_NOT_EXIST);
        }
        return collectRelationMapper.queryIdListBySetId(setId);
    }

    @Override
    public Integer queryCountBySetId(Long setId) {
        if (setId == null || setId < 0) {
            throw new BizException(BizErrorCodeEnum.DATA_NOT_EXIST);
        }
        return queryIdListBySetId(setId).size();
    }

    @Override
    public CollectRelationPO queryCollectRelationByRelation(Long setId, Integer relationType, Long relationId) {
        if (setId == null || relationType == null || relationId == null) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        return collectRelationMapper.queryCollectRelationByRelation(setId, relationType, relationId);
    }

    @Override
    public List<CollectRelationPO> queryCollectRelationListBySetId(Long setId) {
        if (setId == null || setId < 0) {
            throw new BizException(BizErrorCodeEnum.DATA_NOT_EXIST);
        }
        return collectRelationMapper.queryCollectRelationListBySetId(setId);
    }

    @Override
    public List<Long> querySetIdListByRelationId(Long relationId) {
        if (relationId == null || relationId < 0) {
            return Collections.emptyList();
        }
        return collectRelationMapper.querySetIdListByRelationId(relationId);
    }
}
